import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import MyMap from "@/views/map/MyMap";
import store from  '@/store/index';
import Register from "@/views/userInfo/Register";
import MapList from "@/views/map/MapList";
import AMapLoader from "@amap/amap-jsapi-loader";
import UserSet from "@/views/set/UserSet";
import ArticleList from "@/views/article/ArticleList.vue";
Vue.use(VueRouter)


const routes = [
  {
    path: '/',
    name: 'home',
    redirect:ArticleList,
    component: HomeView,
    children: [
      {
        path: '/map/mymap',
        name: 'MyMap',
        // component: MyMap
        component: ()=>import("@/views/map/MyMap"),
        /*beforeEnter:async (to, from, next) => {

          if (typeof AMap === "undefined") {
            /!*let result = await AMapLoader.load({
              key: "99df1adfafd916e84e7b953d75c626d7",             // 申请好的Web端开发者Key，首次调用 load 时必填
              version: "2.0",      // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
              plugins: [''],       // 需要使用的的插件列表，如比例尺'AMap.Scale'等
            }).then(() => {
              console.log("zhixing")
            })*!/
          }
          console.log("next")
          next();
        }*/
      },{
        path: '/map/maplist',
        name: 'MapList',
        component: MapList
      },{
        path: '/map/fileupload',
        name: 'MyFile',
        component: ()=>import('@/views/file/UpFile')
      },{
        path: '/map/filelist',
        name: 'FileList',
        component: ()=>import('@/views/file/FileList')
        },{
        path:'/map/articleList',
        name: 'ArticleList',
        component: ()=>import('@/views/article/ArticleList')
      },{
        path: '/map/travel',
        name: 'NameTravel',
        component: ()=>import('@/views/map/MapTravel')
      },{
        path:'/article/edit',
        name:'ArticleEdit',
        //todo 切换编辑器

        // component:()=>import('@/views/article/CkEditor5/ArticleEdit')
        component:()=>import('@/views/article/wangEditor/WangEditor')
      }
    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/about/AboutView.vue'),
    children:[
      {
        path: '/about/transition',
        name: 'transition',
        component: ()=>import('@/views/about/transition/Transition')
      },
      {
        path: '/about/basic',
        name: 'basic',
        component: ()=>import('@/views/about/Layout/basic')
      },
      {
        path: '/about/响应式布局',
        name: '响应式布局',
        component: ()=>import('@/views/about/Layout/响应式布局')
      },
      {
        path: '/about/container',
        name: '容器示例',
        component: ()=>import('@/views/about/Container/index')
      }
    ]
  },
  {
    name:'Login',
    path:'/login',
    component: ()=>import('@/views/userInfo/Login')
  },
  {
    name:'Register',
    path:'/register',
    component: ()=>import('@/views/userInfo/Register')
  },{
    name:'Set',
    path: '/set',
    redirect: UserSet,
    component: ()=>import('@/views/set/set'),
    children: [
      {
        name:'FeatureSet',
        path: 'feature_set',
        component: ()=>import('@/views/set/FeatureSet')
      },
      {
        name:'UserSet',
        path: 'user_set',
        component: ()=>import('@/views/set/UserSet')
      },
      {
        path: 'VisionInfo',
        name: 'VisionInfo',
        component: ()=>import('@/views/set/Info/VisionInfo')
      },
    ]
  }
]

const router = new VueRouter({
  // mode: 'history',
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach(async (to, from, next) => {
  store.dispatch("getUserInfo").then(() => {
    const isLogin = store.getters.isLogin;
    if (isLogin) {
      if (to.name === "Login" || to.name === Register.name){
        next(ArticleList);
      }else{
        next();
      }
    }else{
      if (to.name === "Login" || to.name === Register.name) {
        next();
      }else{
        localStorage.setItem("nextRoute", to.name);
        next({
          name: "Login"
        })
      }
    }
  }).catch(ex => {
    localStorage.setItem("nextRoute", to.name);
    next({
      name: "Login"
    })
    console.log(ex);
  })
})
export default router
